package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoleBean;
import com.project.mapper.IPowerMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper mapper;
    @Autowired
    private IPowerMapper powerMapper;
    @Override
    public IPage<RoleBean> findByItem(Integer pageNo) {
        return mapper.selectPage(new Page(pageNo,5),null);
    }

    @Override
    public List<RoleBean> findAll() {
        return mapper.selectList(null);
    }

    @Override
    public RoleBean findById(Integer id) {
        RoleBean role = mapper.selectById(id);
        role.setPowerList(powerMapper.findByRoleId(id));
        return role;
    }

    @Override
    public void add(RoleBean role, Integer[] powerId) {
     mapper.insert(role);
     mapper.addPower(role.getId(),powerId);
    }

    @Override
    public void del(Integer roleId) {
      mapper.delPower(roleId);
      mapper.delUser(roleId);
      mapper.deleteById(roleId);
    }

    @Override
    public void update(Integer roleId, Integer[] powerId) {
        mapper.delPower(roleId);
        mapper.addPower(roleId,powerId);
    }
}
